package com.xianbeiying.app.ckgl.widght.dapengView;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.xianbeiying.app.ckgl.R;
import com.xianbeiying.app.ckgl.bean.DevicesBean;

import java.util.ArrayList;
import java.util.List;

/**
 * 大棚控制显示数据
 */
public class DpView extends InstanceDpdate{

    private ArrayList<View> t_list = new ArrayList<View>();

    private int sid;

    private int size;

    List<DevicesBean.GREENHOUSEBean> GREENHOUSE;

    private LinearLayout lineview;

    private Context context;

    public DpView(List<DevicesBean.GREENHOUSEBean> GREENHOUSE, int size, int sid, LinearLayout lineview, Context context){

        this.GREENHOUSE = GREENHOUSE;

        this.sid = sid;

        this.lineview = lineview;

        this.context = context;

        this.size=size;

    }

    //设置数据
    public void setDpdata(){
        t_list.clear();
        lineview.removeAllViews();
        int size=GREENHOUSE.size();
        for (int i = 0; i < size; i++){
            //每个textview的宽度
            View view = LayoutInflater.from(context).inflate(R.layout.fragment_va_item, null);
            LinearLayout ly_va_cicy = (LinearLayout) view.findViewById(R.id.ly_va_cicy);
            final TextView tv_va_cicy = (TextView) view.findViewById(R.id.tv_va_cicy);
//            tv_va_cicy.setText((i+1)+"号棚");
            tv_va_cicy.setText(GREENHOUSE.get(i).getGreenhouseName());
            if (i == sid){
                ly_va_cicy.setSelected(true);
                tv_va_cicy.setSelected(true);
            }
            //添加集合
            t_list.add(view);
            //绑定
            ly_va_cicy.setTag(i);
            lineview.addView(view);
            ly_va_cicy.setOnClickListener(new View.OnClickListener(){
                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    int m = (Integer) v.getTag();
                    int length = t_list.size();
                    for (int i = 0; i < length; i++) {
                        if (i == m) {
                            t_list.get(m).findViewById(R.id.ly_va_cicy).setSelected(true);
                            t_list.get(m).findViewById(R.id.tv_va_cicy).setSelected(true);
                            DpView.this.onDpdata(m, true);
                            //数据变化
                        } else {
                            t_list.get(i).findViewById(R.id.ly_va_cicy).setSelected(false);
                            t_list.get(i).findViewById(R.id.tv_va_cicy).setSelected(false);
                            //数据变化
                        }
                    }
                }
            });
        }

    }
}
